<template>
  <div :id="id" ref="boxwrap" :class="className" :style="{height:height,width:width}" />
</template>

<script>
import resize from '@/components/Charts/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    }
  },
  data() {
    return {
      chart: null,
      fontColor: '#bebebe',
      color: ['deepskyblue', 'orange', 'purple']
    }
  },
  mounted() {
    this.domResize()
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = this.$echarts.init(document.getElementById(this.id))

      this.chart.setOption({
        color: this.color,
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          orient: 'vertical',
          top: '3%',
          right: '1%',
          textStyle: {
            fontSize: 14,
            color: this.fontColor
          }
        },
        grid: {
          top: '20%',
          left: '5%',
          right: '20%',
          bottom: '15%',
          containLabel: true
        },
        dataZoom: [{
          show: true,
          type: 'inside',
          bottom: 15,
          height: 10,
          startValue: 0,
          endValue: 5
        }, {
          show: true,
          bottom: 15,
          height: 10,
          startValue: 0,
          endValue: 5
        }],
        xAxis: [{
          type: 'category',
          boundaryGap: false,
          axisLabel: {
            color: this.fontColor
          },
          data: ['7/1', '7/2', '7/3', '7/4', '7/5', '7/6', '7/7', '7/8', '7/9', '7/10', '7/11', '7/12']
        }],
        yAxis: [{
          type: 'value',
          name: '次数',
          nameTextStyle: {
            color: this.fontColor
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            color: this.fontColor
          },
          splitLine: {
            lineStyle: {
              color: 'rgba(255,255,255,0.1)'
            }
          }
        }],
        series: [{
          name: 'A',
          type: 'line',
          smooth: true,
          label: {
            show: true,
            position: 'top',
            color: this.fontColor
          },
          data: [12, 13, 10, 13, 9, 3, 10, 10, 19, 15, 8, 9]
        }, {
          name: 'B',
          type: 'line',
          smooth: true,
          label: {
            show: true,
            position: 'top',
            color: this.fontColor
          },
          data: [2, 8, 1, 4, 9, 3, 10, 19, 4, 9, 3, 3]
        }, {
          name: 'C',
          type: 'line',
          smooth: true,
          label: {
            show: true,
            position: 'top',
            color: this.fontColor
          },
          data: [3, 2, 1, 4, 9, 3, 2, 8, 1, 4, 10, 13]
        }]
      })
    },
    domResize() {
      const that = this
      const dom = this.$refs.boxwrap
      this.$erd.listenTo(dom, function(element) {
        that.chart.resize()
      })
    }
  }
}
</script>
